package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.UserService;
import com.jt.util.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService;
    @Reference(check = false,timeout = 3000)
    private DubboUserService dubboUserService;
    @Autowired
    private JedisCluster jedisCluster;
    /**
     * 通用页面跳转实现
     */
    @RequestMapping("{Module}")
    public String module(@PathVariable String Module){
        return Module;
    }

    /**
     * 测试httpClient实现业务查询用户信息
     * url:http://www.jt.com/findUserById/7
     * 参数:7
     * 返回对象:user对象
     */
    @RequestMapping("findUserById/{userId}")
    @ResponseBody
    public User findUserById(@PathVariable Long userId){
        return userService.findUserById(userId);
    }

    @RequestMapping("doRegister")
    @ResponseBody
    public SysResult doRegister(User user){
        dubboUserService.insertObject(user);
        return SysResult.success();
    }

    /**
     * 完成用户的登录的操作
     * url:user/doLogin
     * 参数:username password
     * 返回值:SysResult对象 的JSON数据
     * cookie.setMaxAge(0)  立即删除cookie
     * cookie.setMaxAge(-1)  关闭浏览器会话时删除
     *
     * cookie.setPath("/");
     */
    @RequestMapping("doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response){
        //实现用户登录操作
        String ticket = dubboUserService.doLogin(user);
        //校验ticket是否有值
        if(StringUtils.isEmpty(ticket)){
            //用户名或者密码错误
            return SysResult.fail();
        }else {
            //如果用户ticket不为空,则表示登录正确,将数据保存到cookie中即可
            //cookie要求  1.要求7天有效  2.可以再jt.com的域名中共享  3.cokie权限在根目录
            Cookie cookie = new Cookie("JT_TICKET",ticket);
            cookie.setMaxAge(7*24*60*60);
            cookie.setDomain("jt.com");
            cookie.setPath("/");
            response.addCookie(cookie);
           // CookieUtil.addCookie("JT_TICKET",ticket,"/","jt.com",7*24*60*60,response);
            return SysResult.success();
        }
    }

    @RequestMapping("logout")
    public String logout(HttpServletRequest request,HttpServletResponse response){
      /*  //1.如何获取cookie中的数据?
        Cookie[] cookies = request.getCookies();
        //2.校验Cookie数据是否为null
        if(cookies !=null && cookies.length>0){
            for(Cookie cookie : cookies){
                if("JT_TICKET".equalsIgnoreCase(cookie.getName())){
                   String jtTicket = cookie.getValue();
                    jedisCluster.del(jtTicket);
                    //业务需要提前删除Cookie
                    cookie.setDomain("jt.com");
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }
        }*/
        String ticket = CookieUtil.getValue("JT_TICKET",request);
        if(!StringUtils.isEmpty(ticket)){
            //1.删除redis
            jedisCluster.del(ticket);
            //2.删除cookie
            CookieUtil.deleteCookie("JT_TICKET","/","jt.com",response);
        }
        return "redirect:/";
    }
}
